home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************/
- /* */
- /* Application: Direct Pixel Access */
- /* */
- /* Description: This snippet shows one example of how to directly */
- /* change the pixel values stored in a pixel image. */
- /* The original pixel image is obtained from a 'icl8' */
- /* resource. Only the first 20 columns of the first */
- /* 20 rows of the 'icl8' image is used. */
- /* */
- /* Files: Direct Pixel Access.π */
- /* Direct Pixel Access.π.rsrc */
- /* Direct Pixel Access.c */
- /* */
- /* Programmer: Edgar Lee */
- /* Organization: Apple Computer, Inc. */
- /* Department: Developer Technical Support, DTS */
- /* Language: C (Think C version 5.0.2) */
- /* Date Created: 03-06-92 */
- /* */
- /****************************************************************************/
-
- /* Constant Declarations */
-
- #define WWIDTH 600
- #define WHEIGHT 400
-
- #define WLEFT (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
- #define WTOP (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
-
- /* Global Variable Definitions */
-
- WindowPtr gWindow;
- PixMapHandle gPixmap;
-
- void initMac();
-
- void createWindow();
- void createOffscreen();
- void drawPixelImageData();
- void doEventLoop();
-
- main()
- {
- initMac();
-
- createWindow();
- createOffscreen();
-
- doEventLoop();
- }
-
- void initMac()
- {
- MaxApplZone();
-
- InitGraf( &thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( nil );
- InitCursor();
- FlushEvents( 0, everyEvent );
- }
-
- void createWindow()
- {
- Rect rect;
-
- SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
- gWindow = NewCWindow( 0L, &rect, "\pDirect Pixel Access", true, documentProc,
- (WindowPtr)-1L, true, 0L );
- SetPort( gWindow );
-
- TextFont( geneva );
- TextSize( 9 );
- TextMode( srcXor );
- }
-
- void createOffscreen()
- {
- Rect rect;
- Handle iclHandle;
-
- SetRect( &rect, 0, 0, 32, 32 );
-
- /* Create offscreen pixmap image using an 'icl8' icon resource. */
-
- iclHandle = GetResource( 'icl8', 129 );
- HLock( iclHandle );
- HNoPurge( iclHandle );
-
- gPixmap = (PixMapHandle)NewHandle( sizeof( PixMap ) );
-
- (**gPixmap).baseAddr = *iclHandle;
- (**gPixmap).rowBytes = ((32 * 8) / 8) | 0x8000;
- (**gPixmap).bounds = rect;
- (**gPixmap).pmVersion = 0;
- (**gPixmap).packType = 0;
- (**gPixmap).packSize = 0;
- (**gPixmap).hRes = 72;
- (**gPixmap).vRes = 72;
- (**gPixmap).pixelSize = 8;
- (**gPixmap).planeBytes = 0;
- (**gPixmap).pmReserved = 0;
- (**gPixmap).pixelType = 0;
- (**gPixmap).cmpCount = 1;
- (**gPixmap).cmpSize = 8;
- (**gPixmap).pmTable = GetCTable( 8 );
-
- /* Give a unique seed for the pixmap's colortable. */
- (**(**gPixmap).pmTable).ctSeed = GetCTSeed();
- }
-
- void drawPixelImageData()
- {
- int row, col;
- Rect rect;
- unsigned char value;
- char *image;
- int index = 0;
- Str255 string;
- RGBColor color = { 32000, 32000, 32000 };
- Byte mode;
-
- ForeColor( blackColor );
-
- /* For this example, let's just use only the upper left corner of the image. */
- SetRect( &rect, 0, 0, 20, 20 );
-
- /* Set the pointer to the beginning of the pixel image. */
- image = GetPixBaseAddr( gPixmap );
-
- /*********************************************************************************/
- /* Switch into 32-bit addressing mode before accessing the pixel image directly. */
- /*********************************************************************************/
-
- mode = true32b;
- SwapMMUMode( &mode );
-
- /*****************************************************************/
- /* For this example, let's set the pixel values of the left half */
- /* of the image to half their original values. */
- /*****************************************************************/
-
- for (row = 0; row < rect.bottom; row++)
- {
- /* Loop through the first 10 columns of the pixel image. */
- for (index = 0, col = 0; col < rect.right / 2; col++)
- {
- /* Set the value at this index to half its value. */
- value = (unsigned char)*(image + index);
- *(image + index) = value / 2;
-
- index++;
- }
-
- /* Increment the pointer to the next row of the pixel image. */
- image += ((**gPixmap).rowBytes & 0x7fff);
- }
-
- /********************************************************************/
- /* Switch back to 24-bit addressing when done accessing the pixels. */
- /********************************************************************/
-
- SwapMMUMode( &mode );
-
- /* Draw the offscreen image to the screen to see what it looks like. */
- CopyBits( (BitMap *)*gPixmap, &gWindow->portBits, &rect,
- &gWindow->portRect, srcCopy, 0 );
-
- RGBForeColor( &color );
-
- /* Again, set the pointer to the beginning of the pixel image. */
- image = GetPixBaseAddr( gPixmap );
-
- /***************************************************************/
- /* Finally let's display the pixel values on top of the image. */
- /***************************************************************/
-
- /* Loop through the first 20 rows of the pixel image. */
- for (row = 0; row < rect.bottom; row++)
- {
- /* Loop through the first 20 columns of the pixel image. */
- for (index = 0, col = 0; col < rect.right; col++)
- {
- /* Get the value at this index into the pixel image. */
- value = (unsigned char)*(image + index);
-
- MoveTo( col * 30, row * 20 );
- LineTo( col * 30, (row + 1) * 20 );
- LineTo( (col + 1) * 30, (row + 1) * 20 );
-
- MoveTo( (col * 30) + 6, (row * 20) + 14 );
- NumToString( (long)value, &string );
- DrawString( string );
-
- index++;
- }
-
- /* Increment the pointer to the next row of the pixel image. */
- image += ((**gPixmap).rowBytes & 0x7fff);
- }
- }
-
- void doEventLoop()
- {
- EventRecord event;
- WindowPtr window;
- short clickArea;
- Rect screenRect;
-
- for (;;)
- {
- if (WaitNextEvent( everyEvent, &event, 0, nil ))
- {
- if (event.what == mouseDown)
- {
- clickArea = FindWindow( event.where, &window );
-
- if (clickArea == inDrag)
- {
- screenRect = (**GetGrayRgn ()).rgnBBox;
- DragWindow( window, event.where, &screenRect );
- }
- else if (clickArea == inContent)
- {
- if (window != FrontWindow())
- SelectWindow( window );
- }
- else if (clickArea == inGoAway)
- if (TrackGoAway( window, event.where ))
- return;
- }
- else if (event.what == updateEvt)
- {
- window = (WindowPtr)event.message;
- SetPort( window );
-
- BeginUpdate( window );
- drawPixelImageData();
- EndUpdate( window );
- }
- }
- }
- }